<!DOCTYPE html>



  


<html class="theme-next mist use-motion" lang="zh-Hans">
<head><meta name="generator" content="Hexo 3.9.0">
  <meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<meta name="theme-color" content="#222">









<meta http-equiv="Cache-Control" content="no-transform">
<meta http-equiv="Cache-Control" content="no-siteapp">
















  
  
  <link href="/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css">







<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css">

<link href="/css/main.css?v=5.1.4" rel="stylesheet" type="text/css">


  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png?v=5.1.4">


  <link rel="mask-icon" href="/images/logo.svg?v=5.1.4" color="#222">





  <meta name="keywords" content="replugin插件化,">










<meta name="description" content="1、replugin插件化1.1、简介 RePlugin是一套完整的、稳定的、适合全面使用的，占坑类插件化方案。我们“逐词”拆开来解释这个定义：  完整的：让插件运行起来“像单品那样”，支持大部分特性 稳定的：如此灵活完整的情况下，其框架崩溃率仅为业内很低的“万分之一” 适合全面使用的：其目的是让应用内的“所有功能皆为插件” 占坑类：以稳定为前提的Manifest占坑思路 插件化方案：基于Andr">
<meta name="keywords" content="replugin插件化">
<meta property="og:type" content="article">
<meta property="og:title" content="replugin插件化">
<meta property="og:url" content="https://xxyuan.net/2019/03/19/RePlugin插件化开发/index.html">
<meta property="og:site_name" content="晓远Blog">
<meta property="og:description" content="1、replugin插件化1.1、简介 RePlugin是一套完整的、稳定的、适合全面使用的，占坑类插件化方案。我们“逐词”拆开来解释这个定义：  完整的：让插件运行起来“像单品那样”，支持大部分特性 稳定的：如此灵活完整的情况下，其框架崩溃率仅为业内很低的“万分之一” 适合全面使用的：其目的是让应用内的“所有功能皆为插件” 占坑类：以稳定为前提的Manifest占坑思路 插件化方案：基于Andr">
<meta property="og:locale" content="zh-Hans">
<meta property="og:image" content="https://xxyuan.net/2019/03/19/RePlugin插件化开发/RePlugin插件化开发/1550562768152.png">
<meta property="og:updated_time" content="2019-11-16T14:20:50.141Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="replugin插件化">
<meta name="twitter:description" content="1、replugin插件化1.1、简介 RePlugin是一套完整的、稳定的、适合全面使用的，占坑类插件化方案。我们“逐词”拆开来解释这个定义：  完整的：让插件运行起来“像单品那样”，支持大部分特性 稳定的：如此灵活完整的情况下，其框架崩溃率仅为业内很低的“万分之一” 适合全面使用的：其目的是让应用内的“所有功能皆为插件” 占坑类：以稳定为前提的Manifest占坑思路 插件化方案：基于Andr">
<meta name="twitter:image" content="https://xxyuan.net/2019/03/19/RePlugin插件化开发/RePlugin插件化开发/1550562768152.png">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Mist',
    version: '5.1.4',
    sidebar: {"position":"left","display":"post","offset":12,"b2t":false,"scrollpercent":false,"onmobile":false},
    fancybox: true,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="https://xxyuan.net/2019/03/19/RePlugin插件化开发/">





  <title>replugin插件化 | 晓远Blog</title>
  





  <script type="text/javascript">
    var _hmt = _hmt || [];
    (function() {
      var hm = document.createElement("script");
      hm.src = "https://hm.baidu.com/hm.js?b7fbd9803fe7b8df6faf867c7b94ce2a";
      var s = document.getElementsByTagName("script")[0];
      s.parentNode.insertBefore(hm, s);
    })();
  </script>




</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/" class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">晓远Blog</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle"></p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br>
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/tags/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-tags"></i> <br>
            
            标签
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/categories/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-th"></i> <br>
            
            分类
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/archives/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br>
            
            归档
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="https://xxyuan.net/2019/03/19/RePlugin插件化开发/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="晓远">
      <meta itemprop="description" content>
      <meta itemprop="image" content="/images/avatar.gif">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="晓远Blog">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">replugin插件化</h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2019-03-19T00:12:31+08:00">
                2019-03-19
              </time>
            

            

            
          </span>

          
            <span class="post-category">
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/开发/" itemprop="url" rel="index">
                    <span itemprop="name">开发</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <h4 id="1、replugin插件化"><a href="#1、replugin插件化" class="headerlink" title="1、replugin插件化"></a>1、replugin插件化</h4><h5 id="1-1、简介"><a href="#1-1、简介" class="headerlink" title="1.1、简介"></a>1.1、简介</h5><blockquote>
<p>RePlugin是一套完整的、稳定的、适合全面使用的，占坑类插件化方案。我们“逐词”拆开来解释这个定义：</p>
<ul>
<li><strong>完整的</strong>：让插件运行起来“像单品那样”，支持大部分特性</li>
<li><strong>稳定的</strong>：如此灵活完整的情况下，其<strong>框架崩溃率仅为业内很低的“万分之一”</strong></li>
<li><strong>适合全面使用的</strong>：其目的是让应用内的“<strong>所有功能皆为插件</strong>”</li>
<li><strong>占坑类</strong>：以稳定为前提的Manifest占坑思路</li>
<li><strong>插件化方案</strong>：基于Android原生API和语言来开发，充分利用原生特性</li>
</ul>
</blockquote>
<a id="more"></a>
<h5 id="1-2、相关链接"><a href="#1-2、相关链接" class="headerlink" title="1.2、相关链接"></a>1.2、相关链接</h5><blockquote>
<p>githup:<a href="https://github.com/Qihoo360/RePlugin/wiki" target="_blank" rel="noopener">https://github.com/Qihoo360/RePlugin/wiki</a></p>
</blockquote>
<h4 id="2、宿主host"><a href="#2、宿主host" class="headerlink" title="2、宿主host"></a>2、宿主host</h4><h5 id="2-1、接入gradle配置"><a href="#2-1、接入gradle配置" class="headerlink" title="2.1、接入gradle配置"></a>2.1、接入gradle配置</h5> <figure class="highlight groovy"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//host依赖</span></span><br><span class="line">implementation <span class="string">'com.qihoo360.replugin:replugin-host-lib:2.3.1'</span></span><br></pre></td></tr></table></figure>
<figure class="highlight groovy"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//宿主的配置</span></span><br><span class="line">apply <span class="string">plugin:</span> <span class="string">'replugin-host-gradle'</span></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 配置项均为可选配置，默认无需添加</span></span><br><span class="line"><span class="comment"> * 更多可选配置项参见replugin-host-gradle的RepluginConfig类</span></span><br><span class="line"><span class="comment"> * 可更改配置项参见 自动生成RePluginHostConfig.java</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line">repluginHostConfig &#123;</span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 是否使用 AppCompat 库</span></span><br><span class="line"><span class="comment">     * 不需要个性化配置时，无需添加</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    useAppCompat = <span class="literal">true</span></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 背景不透明的坑的数量</span></span><br><span class="line"><span class="comment">     * 不需要个性化配置时，无需添加</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line"><span class="comment">//    countNotTranslucentStandard = 6</span></span><br><span class="line"><span class="comment">//    countNotTranslucentSingleTop = 2</span></span><br><span class="line"><span class="comment">//    countNotTranslucentSingleTask = 3</span></span><br><span class="line"><span class="comment">//    countNotTranslucentSingleInstance = 2</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h5 id="2-2、启动replugin插件"><a href="#2-2、启动replugin插件" class="headerlink" title="2.2、启动replugin插件"></a>2.2、启动replugin插件</h5><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">if</span> (RePlugin.isPluginInstalled(<span class="string">"replugin"</span>)) &#123;</span><br><span class="line">    RePlugin.startActivity(GoPluginActivity.<span class="keyword">this</span>,</span><br><span class="line">            RePlugin.createIntent(<span class="string">"replugin"</span>, <span class="string">"com.xxyuan.replugin.MainActivity"</span>));</span><br><span class="line">&#125; <span class="keyword">else</span> &#123;</span><br><span class="line">    Toast.makeText(GoPluginActivity.<span class="keyword">this</span>, <span class="string">"You must install replugin first!"</span>, Toast.LENGTH_SHORT).show();</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h5 id="2-3、Application的初始化"><a href="#2-3、Application的初始化" class="headerlink" title="2.3、Application的初始化"></a>2.3、Application的初始化</h5><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> com.xxyuan.repluginplatform.base;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> android.content.Context;</span><br><span class="line"><span class="keyword">import</span> android.content.res.Configuration;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> com.qihoo360.replugin.RePlugin;</span><br><span class="line"><span class="keyword">import</span> com.qihoo360.replugin.RePluginApplication;</span><br><span class="line"><span class="keyword">import</span> com.qihoo360.replugin.RePluginCallbacks;</span><br><span class="line"><span class="keyword">import</span> com.qihoo360.replugin.RePluginConfig;</span><br><span class="line"><span class="keyword">import</span> com.xxyuan.repluginplatform.BuildConfig;</span><br><span class="line"><span class="keyword">import</span> com.xxyuan.repluginplatform.http.InitOkgo;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * author:xuxiaoyuan</span></span><br><span class="line"><span class="comment"> * date:2019/1/18</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Application</span> <span class="keyword">extends</span> <span class="title">RePluginApplication</span> </span>&#123;</span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">protected</span> <span class="keyword">void</span> <span class="title">attachBaseContext</span><span class="params">(Context base)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">super</span>.attachBaseContext(base);</span><br><span class="line">        RePlugin.App.attachBaseContext(<span class="keyword">this</span>);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">onCreate</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        <span class="keyword">super</span>.onCreate();</span><br><span class="line">        RePlugin.App.onCreate();</span><br><span class="line">    &#125;</span><br><span class="line">   </span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">protected</span> RePluginConfig <span class="title">createConfig</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        RePluginConfig c = <span class="keyword">new</span> RePluginConfig();</span><br><span class="line">        <span class="comment">// 允许“插件使用宿主类”。默认为“关闭”</span></span><br><span class="line">        c.setUseHostClassIfNotFound(<span class="keyword">true</span>);</span><br><span class="line">        <span class="comment">// FIXME RePlugin默认会对安装的外置插件进行签名校验，这里先关掉，避免调试时出现签名错误</span></span><br><span class="line">        c.setVerifySign(<span class="keyword">false</span>);</span><br><span class="line">        c.setPrintDetailLog(BuildConfig.DEBUG);</span><br><span class="line">        c.setUseHostClassIfNotFound(<span class="keyword">true</span>);</span><br><span class="line">        <span class="comment">// 针对“安装失败”等情况来做进一步的事件处理</span></span><br><span class="line">        c.setEventCallbacks(<span class="keyword">new</span> HostEventCallbacks(<span class="keyword">this</span>));</span><br><span class="line">        c.setMoveFileWhenInstalling(<span class="keyword">true</span>);</span><br><span class="line">        <span class="comment">// FIXME 若宿主为Release，则此处应加上您认为"合法"的插件的签名，例如，可以写上"宿主"自己的。</span></span><br><span class="line">        <span class="comment">// RePlugin.addCertSignature("AAAAAAAAA");</span></span><br><span class="line"></span><br><span class="line">        <span class="keyword">return</span> c;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">protected</span> RePluginCallbacks <span class="title">createCallbacks</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">new</span> HostCallbacks(<span class="keyword">this</span>);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">onLowMemory</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        <span class="keyword">super</span>.onLowMemory();</span><br><span class="line">        RePlugin.App.onLowMemory();</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">onTrimMemory</span><span class="params">(<span class="keyword">int</span> level)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">super</span>.onTrimMemory(level);</span><br><span class="line">        RePlugin.App.onTrimMemory(level);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">onConfigurationChanged</span><span class="params">(Configuration config)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">super</span>.onConfigurationChanged(config);</span><br><span class="line">        RePlugin.App.onConfigurationChanged(config);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h5 id="2-4、安装更新replugin插件"><a href="#2-4、安装更新replugin插件" class="headerlink" title="2.4、安装更新replugin插件"></a>2.4、安装更新replugin插件</h5><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">startRePlugin</span><span class="params">(Context context, String pluginName, String apkPath)</span> </span>&#123;</span><br><span class="line">    <span class="comment">//安装插件过程</span></span><br><span class="line">    PluginInfo pluginInfo = RePlugin.getPluginInfo(pluginName);</span><br><span class="line">    <span class="comment">//插件文件，只有存在就进行安装或者更新</span></span><br><span class="line">    File file = <span class="keyword">new</span> File(apkPath);</span><br><span class="line">    <span class="comment">//判断是否已经安装插件</span></span><br><span class="line">    <span class="keyword">if</span> (pluginInfo == <span class="keyword">null</span>) &#123;</span><br><span class="line">        <span class="comment">//插件未安装的情况</span></span><br><span class="line">        <span class="keyword">if</span> (!file.exists()) &#123;</span><br><span class="line">            Toast.makeText(context, <span class="string">"插件安装失败，插件文件不存在"</span>, Toast.LENGTH_SHORT).show();</span><br><span class="line">        &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">            <span class="comment">//安装插件</span></span><br><span class="line">            PluginInfo pluginInfo1 = RePlugin.install(apkPath);</span><br><span class="line">            <span class="keyword">if</span> (pluginInfo1 == <span class="keyword">null</span>) &#123;</span><br><span class="line">                Toast.makeText(context, <span class="string">"插件安装失败，安装出错"</span>, Toast.LENGTH_SHORT).show();</span><br><span class="line">            &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">                Toast.makeText(context, <span class="string">"插件安装成功"</span>, Toast.LENGTH_SHORT).show();</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">    &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">        <span class="comment">//插件已安装,是否需要升级，判断条件是file是否为空</span></span><br><span class="line">        <span class="keyword">if</span> (file.exists()) &#123;</span><br><span class="line">            PluginInfo pluginInfo1 = RePlugin.install(file.getAbsolutePath());</span><br><span class="line">            <span class="keyword">if</span> (pluginInfo1 == <span class="keyword">null</span>) &#123;</span><br><span class="line">                Toast.makeText(context, <span class="string">"插件升级失败"</span>, Toast.LENGTH_SHORT).show();</span><br><span class="line">            &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">                Toast.makeText(context, <span class="string">"插件升级成功"</span>, Toast.LENGTH_SHORT).show();</span><br><span class="line">            &#125;</span><br><span class="line">        &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">            Toast.makeText(context, <span class="string">"插件已安装"</span>, Toast.LENGTH_SHORT).show();</span><br><span class="line">            RePlugin.preload(pluginInfo);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h4 id="3、插件replugin"><a href="#3、插件replugin" class="headerlink" title="3、插件replugin"></a>3、插件replugin</h4><h5 id="3-1、接入gradle配置"><a href="#3-1、接入gradle配置" class="headerlink" title="3.1、接入gradle配置"></a>3.1、接入gradle配置</h5><figure class="highlight groovy"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">implementation <span class="string">'com.qihoo360.replugin:replugin-plugin-lib:2.3.1'</span></span><br></pre></td></tr></table></figure>
<figure class="highlight groovy"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//插件配置</span></span><br><span class="line">apply <span class="string">plugin:</span> <span class="string">'replugin-plugin-gradle'</span></span><br><span class="line"></span><br><span class="line">repluginPluginConfig &#123;</span><br><span class="line"></span><br><span class="line"><span class="comment">//插件名</span></span><br><span class="line">    pluginName = <span class="string">"replugin"</span></span><br><span class="line"><span class="comment">//宿主app的包名</span></span><br><span class="line">    hostApplicationId = <span class="string">"com.xxyuan.repluginplatform"</span></span><br><span class="line"><span class="comment">//宿主app的启动activity</span></span><br><span class="line">    hostAppLauncherActivity = <span class="string">"com.xxyuan.repluginplatform.MainActivity"</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h5 id="3-2、注意PS"><a href="#3-2、注意PS" class="headerlink" title="3.2、注意PS:"></a>3.2、注意PS:</h5><blockquote>
<p>打包以后有两种方式，最简单的就是直接改名为jar结尾，若是打包为apk结尾需要代码中install一下这个文件，若是用jar包，可以直接在资源文件夹中放置，宿主打包直接安装，生成plugininfo</p>
</blockquote>
<h4 id="4、插件化的原理"><a href="#4、插件化的原理" class="headerlink" title="4、插件化的原理"></a>4、插件化的原理</h4><h5 id="4-1、类加载器ClassLoader"><a href="#4-1、类加载器ClassLoader" class="headerlink" title="4.1、类加载器ClassLoader"></a>4.1、类加载器ClassLoader</h5><blockquote>
<p>什么是ClassLoader？干什么用？</p>
<p>就是用来加载Class文件到JVM，以供程序使用的。</p>
<p>ClassLoader不是用java语言所编写的，而是JVM实现的一部分。</p>
<p>当运行一个程序的时候，JVM启动，运行bootstrap classloader，该ClassLoader加载java（ExtClassLoader和AppClassLoader也在此时被加载），然后调用ExtClassLoader加载扩展API，最后AppClassLoader加载CLASSPATH目录下定义的Class，这就是一个程序最基本的加载流程。  </p>
</blockquote>
<h5 id="4-2、APP的classloader"><a href="#4-2、APP的classloader" class="headerlink" title="4.2、APP的classloader"></a>4.2、APP的classloader</h5><blockquote>
<p>一个App中所有的ClassLoader都联系了起来。当加载类时，如果当前ClassLoader未加载此类，就查询ParentLoader是否加载过，一直往上查找，如果存在就返回，如果都没有，就执行该Loader去执行加载工作。这样避免了类重复加载的浪费。其中常见的Loader有：</p>
<ul>
<li>BootClassLoader 是系统启动时创建的，一般不需要用到。</li>
<li>PathClassLoader 是应用启动时创建的，只能加载内部dex。</li>
<li>DexClassLoader 可以加载外部的dex。</li>
</ul>
<p><strong>RePlugin中存在两个主要ClassLoaer：</strong></p>
<ul>
<li><strong>1、RePluginClassLoader：</strong> 宿主App中的Loader，继承PathClassLoader，也是唯一Hook住系统的Loader。</li>
<li><strong>2、PluginDexClassLoader：</strong> 加载插件的Loader，继承DexClassLoader。用来做一些“更高级”的特性。</li>
</ul>
</blockquote>
<h5 id="4-3、插件RepluginClassLoader"><a href="#4-3、插件RepluginClassLoader" class="headerlink" title="4.3、插件RepluginClassLoader"></a>4.3、插件RepluginClassLoader</h5><blockquote>
<p>在应用启动的时候，Replugin使用<code>RepluginClassLoader</code>将系统的<code>PathClassLoader</code>替换掉，并且只篡改了<code>loadClass</code>方法的行为，用于加载插件的类，后面我们会详细讲解。每一个插件都会有一个PluginDexClassLoader，<code>RepluginClassLoader</code>会调用插件的<code>PluginDexClassLoader</code>来加载插件中的类与资源。</p>
</blockquote>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">protected</span> Class&lt;?&gt; loadClass(String className, <span class="keyword">boolean</span> resolve) <span class="keyword">throws</span> ClassNotFoundException &#123;</span><br><span class="line">  Class&lt;?&gt; c = <span class="keyword">null</span>;</span><br><span class="line">  c = PMF.loadClass(className, resolve);   <span class="comment">//主力这里就是Hook点，先使用插件的</span></span><br><span class="line">  <span class="keyword">if</span> (c != <span class="keyword">null</span>) &#123;                         <span class="comment">//PluginDexClassLoader加载</span></span><br><span class="line">      <span class="keyword">return</span> c;</span><br><span class="line">  &#125;</span><br><span class="line">  <span class="comment">//只有在插件没有找到相应的类，才使用系统原来的PathClassLoader加载宿主中的类</span></span><br><span class="line">  <span class="keyword">try</span> &#123;</span><br><span class="line">  c = mOrig.loadClass(className);</span><br><span class="line">      <span class="keyword">return</span> c;</span><br><span class="line">  &#125; <span class="keyword">catch</span> (Throwable e) &#123;</span><br><span class="line">  &#125;</span><br><span class="line">      </span><br><span class="line">  <span class="keyword">return</span> <span class="keyword">super</span>.loadClass(className, resolve);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p><img src="/2019/03/19/RePlugin插件化开发/./RePlugin插件化开发/1550562768152.png" alt="1550562768152"></p>
<h5 id="坑位"><a href="#坑位" class="headerlink" title="坑位"></a>坑位</h5><p>Android中<code>Activity</code>和<code>Context</code>都有<code>startActivity</code>函数，为了在插件中能正常启动<code>Activity</code>，我们要将这些函数都屏蔽掉，转而使用<code>Replugin</code>提供的<code>startActivity</code>。</p>
<blockquote>
<p>为什么一定要用Replugin提供startActivity方法呢？因为我们插件的组件在Host的Manifest中是没有声明的，</p>
<p>只能通过坑位来启动，而启动坑位的动作只能在Host中完成。</p>
</blockquote>
<p>所有继承了<code>Activity</code>的类都被强制修改成继承<code>PluginActivity</code>，这个工作是由<code>replugin-plugin-gradle</code>在编译阶段完成的。</p>
<blockquote>
<p>实际上在<code>replugin-plugin-lib</code>中的<code>com.qihoo360.replugin.loader.a</code>包中，你可以找到更多的Activity关的类。</p>
</blockquote>
<h5 id="4-4、启动流程"><a href="#4-4、启动流程" class="headerlink" title="4.4、启动流程"></a>4.4、启动流程</h5><p>插件<code>Activity</code>启动的流程大致是：</p>
<ul>
<li><p>加载目标<code>Activity</code>信息</p>
<p>开始查找<code>Activity</code>信息 —&gt; 找到对应的<code>Pugin</code>信息 —&gt; 解压APK文件 —&gt; 加载Dex文件内容 —&gt; 创建<code>Application</code>对象 —&gt; 创建<code>Entry</code>对象初始化Plugin环境</p>
</li>
<li><p>寻找坑位</p>
<p>查找坑位 —&gt; 将目标<code>Activity</code>与找到的坑位以<code>ActivityState</code>的形式缓存在<code>PluginContainer</code>s中  —&gt; 启动坑位<code>Activity</code> —&gt;  系统调用<code>RepluginClassLoader</code> —&gt; 调用<code>PluginDexClassLoader</code>加载坑位<code>Activity</code>类 —&gt; 通过<code>PluginContainers</code>找到坑位对应的目标<code>Activity</code>类 —&gt; 系统调用<code>PluginActivity</code>的<code>attachBaseContext</code>函数 —&gt; 创建<code>PluginContext</code>对象并替换 —&gt; 目标<code>Activity</code>的正常启动流程（<code>onCreate</code>，<code>onStart</code>，<code>onResume</code>…..)</p>
</li>
</ul>

      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/tags/replugin插件化/" rel="tag"># replugin插件化</a>
          
        </div>
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/2019/01/22/移动端接入weex开发/" rel="next" title="移动端接入weex开发">
                <i class="fa fa-chevron-left"></i> 移动端接入weex开发
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/2019/04/24/docker部署使用odoo/" rel="prev" title="docker部署使用odoo">
                docker部署使用odoo <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          

  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap">
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview-wrap">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview-wrap sidebar-panel">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
            
              <p class="site-author-name" itemprop="name">晓远</p>
              <p class="site-description motion-element" itemprop="description">这个一个关于博客的个人网站</p>
          </div>

          <nav class="site-state motion-element">

            
              <div class="site-state-item site-state-posts">
              
                <a href="/archives/">
              
                  <span class="site-state-item-count">51</span>
                  <span class="site-state-item-name">日志</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-categories">
                <a href="/categories/index.html">
                  <span class="site-state-item-count">3</span>
                  <span class="site-state-item-name">分类</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-tags">
                <a href="/tags/index.html">
                  <span class="site-state-item-count">37</span>
                  <span class="site-state-item-name">标签</span>
                </a>
              </div>
            

          </nav>

          

          

          
          

          
          

          

        </div>
      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-4"><a class="nav-link" href="#1、replugin插件化"><span class="nav-number">1.</span> <span class="nav-text">1、replugin插件化</span></a><ol class="nav-child"><li class="nav-item nav-level-5"><a class="nav-link" href="#1-1、简介"><span class="nav-number">1.1.</span> <span class="nav-text">1.1、简介</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#1-2、相关链接"><span class="nav-number">1.2.</span> <span class="nav-text">1.2、相关链接</span></a></li></ol></li><li class="nav-item nav-level-4"><a class="nav-link" href="#2、宿主host"><span class="nav-number">2.</span> <span class="nav-text">2、宿主host</span></a><ol class="nav-child"><li class="nav-item nav-level-5"><a class="nav-link" href="#2-1、接入gradle配置"><span class="nav-number">2.1.</span> <span class="nav-text">2.1、接入gradle配置</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#2-2、启动replugin插件"><span class="nav-number">2.2.</span> <span class="nav-text">2.2、启动replugin插件</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#2-3、Application的初始化"><span class="nav-number">2.3.</span> <span class="nav-text">2.3、Application的初始化</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#2-4、安装更新replugin插件"><span class="nav-number">2.4.</span> <span class="nav-text">2.4、安装更新replugin插件</span></a></li></ol></li><li class="nav-item nav-level-4"><a class="nav-link" href="#3、插件replugin"><span class="nav-number">3.</span> <span class="nav-text">3、插件replugin</span></a><ol class="nav-child"><li class="nav-item nav-level-5"><a class="nav-link" href="#3-1、接入gradle配置"><span class="nav-number">3.1.</span> <span class="nav-text">3.1、接入gradle配置</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#3-2、注意PS"><span class="nav-number">3.2.</span> <span class="nav-text">3.2、注意PS:</span></a></li></ol></li><li class="nav-item nav-level-4"><a class="nav-link" href="#4、插件化的原理"><span class="nav-number">4.</span> <span class="nav-text">4、插件化的原理</span></a><ol class="nav-child"><li class="nav-item nav-level-5"><a class="nav-link" href="#4-1、类加载器ClassLoader"><span class="nav-number">4.1.</span> <span class="nav-text">4.1、类加载器ClassLoader</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#4-2、APP的classloader"><span class="nav-number">4.2.</span> <span class="nav-text">4.2、APP的classloader</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#4-3、插件RepluginClassLoader"><span class="nav-number">4.3.</span> <span class="nav-text">4.3、插件RepluginClassLoader</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#坑位"><span class="nav-number">4.4.</span> <span class="nav-text">坑位</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#4-4、启动流程"><span class="nav-number">4.5.</span> <span class="nav-text">4.4、启动流程</span></a></li></ol></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; <span itemprop="copyrightYear">2020</span>
  <span class="with-love">
    <i class="fa fa-snowflake-o"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">晓远</span>

  
</div>









        







        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  












  
  
    <script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>
  

  
  
    <script type="text/javascript" src="/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>
  


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.1.4"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.1.4"></script>



  
  

  
  <script type="text/javascript" src="/js/src/scrollspy.js?v=5.1.4"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=5.1.4"></script>



  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.1.4"></script>



  


  




	





  





  












  





  

  

  

  
  

  

  

  

</body>
</html>
